package controllers

import (
	"errors"
	//	"gitee.com/lzqysoft/ilive/common/log"
	"gitee.com/lzqysoft/ilive/models"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
)

type WatchHistoryController struct {
	CommonController
}

func (c *WatchHistoryController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("GetWatchHistory", c.GetWatchHistory)
}

// @Title Post
// @Description create watchhistory
// @Success 201 {int}
// @Failure 403 body is empty
// @router / [post]
func (c *WatchHistoryController) Post() {
	user := c.user
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.(map[string]interface{})
	beego.Debug("watchhistorysdatasdatasdata:", sdata)
	times := sdata["times"].(string)
	wtime, _ := strconv.ParseFloat(times, 64)
	sid := sdata["id"].(string)
	vid, _ := strconv.ParseInt(sid, 10, 64)
	org, _ := models.GetOrganizationById(user.Organization.Id)
	var wh models.WatchHistory
	wh.UserId = user.Id
	wh.UserName = user.Name
	wh.OrgId = org.Id
	wh.OrgName = org.Name
	wh.VideoId = vid
	wh.VideoName = sdata["name"].(string)
	wh.Url = sdata["url"].(string)
	wh.LookTime = time.Now()

	wh.LookLong = int64(wtime)
	wh.LookCount = 1
	videotype := sdata["type"].(string)
	if videotype == "rtmp/flv" {
		wh.Type = 2
	} else {
		wh.Type = 1
	}
	//	vtype := sdata["vtype"].(string)
	//	wh.Type, _ = strconv.ParseInt(vtype, 10, 64)

	wh.Creater = user.Name
	wh.Ctime = wh.LookTime

	wh.Updater = wh.Creater
	wh.Utime = wh.Ctime
	id, err := models.AddWatchHistory(&wh)
	if err != nil {
		c.RetError(ErrDatabase)
	}
	c.RetSuccess(id)
}

// @Title Get All
// @Description get WatchHistory
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.WatchHistory
// @Failure 403
// @router / [get]
func (c *WatchHistoryController) GetAll() {
	var fields []string
	var sortby []string
	var order []string
	var query map[string]string = make(map[string]string)
	var limit int64 = -1
	var offset int64 = 0

	// fields: col1,col2,entity.col3
	if v := c.GetString("fields"); v != "" {
		fields = strings.Split(v, ",")
	}
	// limit: 10 (default is 10)
	if v, err := c.GetInt64("limit"); err == nil {
		limit = v
	}
	// offset: 0 (default is 0)
	if v, err := c.GetInt64("offset"); err == nil {
		offset = v
	}
	// sortby: col1,col2
	if v := c.GetString("sortby"); v != "" {
		sortby = strings.Split(v, ",")
	}
	// order: desc,asc
	if v := c.GetString("order"); v != "" {
		order = strings.Split(v, ",")
	}
	// query: k:v,k:v
	if v := c.GetString("query"); v != "" {
		for _, cond := range strings.Split(v, ",") {
			kv := strings.SplitN(cond, ":", 2)
			if len(kv) != 2 {
				c.Data["json"] = errors.New("Error: invalid query key/value pair")
				c.ServeJSON()
				return
			}
			k, v := kv[0], kv[1]
			query[k] = v
		}
	}
	//query["OrgId"] = string(c.GetSession("userinfo").(models.User).OrgId)
	l, err := models.GetAllWatchHistory(query, fields, sortby, order, offset, limit)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = l
	}
	c.ServeJSON()
}

// @Title GetWacthHistoryl
// @Description 获取自身的历史记录
// @Success 200
// @Failure 403
// @router /GetWatchHistory [get]
func (c *WatchHistoryController) GetWatchHistory() {
	var fields []string
	var sortby []string
	var order []string
	var query map[string]string = make(map[string]string)
	var limit int64 = -1
	var offset int64 = 0
	user := c.user
	var l []interface{}
	query["Isdelete"] = "0"
	query["UserId"] = strconv.FormatInt(user.Id, 10)
	l, err := models.GetAllWatchHistory(query, fields, sortby, order, offset, limit)
	if err != nil {
		c.RetError(ErrNoData)
	}

	c.RetSuccess(l)
}
